I am trying to test a method from a service that performs a http POST request. The thing is that Jasmine is throwing the following error:
Jasmine spec failure list:
Error: Expected one matching request for criteria "Match URL: http://localhost:8080/api/members/1/skills", found none.
Can anyone explain me what is wrong? Actually, the url is correct, so I don't know what is going on.
This is the method I want to test:
postSkillforMember(memberId: string, skillFormBody: any, successCallback: Function, errorCallback: Function) {
console.log(`${this._membersUrl}/${memberId}/skills`);
this._http
.post<Skill>(`${this._membersUrl}/${memberId}/skills`, skillFormBody, {
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
responseType: 'json',
observe: 'body',
})
.subscribe({
next: (value) => {
this.newSkill = value;
},
complete: () => {
successCallback(this.newSkill);
},
error: (error) => { catchError(this.handleError);
errorCallback(error);
}
});
}
This is my it in spec.ts:
it('Testing postSkillforMember() success', () => {
const skillFormBody = {
skill: {
"id": '1',
"skill": ""
},
skillLevel: {
"id": '1',
"skillLevel": ""
}
}
let successCb: boolean = false;
mockSkillAddService.postSkillforMember(memberMockObject.fakeMember.id.toString(), skillFormBody, ()=>{ successCb = true }, ()=>{});
const req = mockHttpTestingController.expectOne(`http://localhost:8080/api/members/1/skills`);
const expectedResponse = new HttpResponse({ status: 201, statusText: 'Created' });
req.event(expectedResponse);
const msg = '201 Created';
req.flush(msg, { status: 201, statusText: 'Created' });
expect(successCb).toBeTruthy();
});
Thank you in advance for your help, I really appreciate it!